// user.js
var api = require('../../api.js');
var app = getApp();
var p = 1;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        BillList: [],
        tabIndex: 0,
        flage: true
    },
    // 返回顶部
    toTop() {
        wx.pageScrollTo({
            scrollTop: 0, // 滚动到页面的目标位置（单位px）,
            duration: 300 // 滚动动画的时长，默认300ms，单位 ms,
        });
    },
    // 点击事件
    onBillItemTap: function (event) {
        var page = this;
        p=1;
        var index = event.currentTarget.dataset['index'];
      console.log(index)
        page.setData({
            tabIndex: index,
            flage: true
        });
        page.getBillList(index, p);
    },
    // 点击加载更多
    getMoreBill() {
        wx.showLoading({
            title: "努力加载中",
            mask: true,
        });
        p++;
        var page = this;
        var type = page.data.tabIndex;
        if (page.data.flage) {
            app.request({
                url: api.default.bill_list,
                data: {
                    type: type,
                    page: p,
                    token: app.globalData.token,
                    // uid: wx.getStorageSync("user_info").user_id
                },
                header: {
                    "Content-Type": "application/x-www-form-urlencoded"
                },
                success: function (res) {
                    if (res.code == 1) {
                        var BillList = page.data.BillList.concat(res.data);
                        page.setData({
                            BillList: BillList
                        });
                    } else {
                        page.setData({
                            flage: false
                        });
                        p=1;
                        wx.showModal({
                            content: '没有更多数据啦',
                            success: function (res) {
                                if (res.confirm) {
                                    return;
                                } else if (res.cancel) {
                                    return;
                                }
                            }
                        })
                    }
                    wx.hideLoading();
                },
            })
        }
        wx.hideLoading();
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var page = this;
        page.getBillList(0, 1);
    },
    // 获取账单列表
    getBillList(type, p) {
        wx.showLoading({
            title: "加载中",
            mask: true,
        });
        var page = this;
        app.request({
            url: api.default.bill_list,
            data: {
                type: type,
                page: p,
                token: app.globalData.token,
                // uid: wx.getStorageSync("user_info").user_id
            },
            header: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            success: function (res) {
              console.log(res,'列表')
                if (res.code == 1) {
                    page.setData({
                        BillList: res.data
                    });
                }else{
                  page.setData({
                    BillList: []
                  });
                }
                wx.hideLoading();
            },
        })
    },
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        wx.stopPullDownRefresh();
        p = 1;
        this.onLoad();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },
});